/*******************************************************************************
 * Copyright (c) 2007 TOPCASED. All rights reserved. This program
 * and the accompanying materials are made available under the terms of the
 * Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: Topcased contributors and others - initial API and implementation
 *******************************************************************************/
/**
 * This is a fork of the UML2Java Plug-in developed by Topcased and released under
 * the Eclipse EPL License. http://www.eclipse.org/legal/epl-v10.html
 * In accordance with the license, I have forked the UML2Java plugin and have adapted
 * it to suit the needs of a UML2QoS instrumentation code generator for PHP.
 * 
 * @author Alexandru Burdusel
 */
package org.topcased.uml2qos;

import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;

import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.Status;
import org.eclipse.osgi.util.NLS;

/**
 * Error log handler class
 * @author Ariel RAMPHUL (Sopra Group - Toulouse)
 */
public class ErrorLogHandler extends Handler
{

    private String mPluginID;
    private ILog mPluginLog;

    /**
     * Error log handler
     * @param loggingPlugin
     */
    public ErrorLogHandler(Plugin loggingPlugin)
    {
        mPluginLog = loggingPlugin.getLog();
        mPluginID = loggingPlugin.getBundle().getSymbolicName();
    }

    /**
     * Publish a LogRecord
     */
    public void publish(LogRecord record)
    {

        int severity = 0;
        Level level = record.getLevel();
        if (Level.CONFIG.equals(level) || Level.INFO.equals(level) || Level.FINE.equals(level)
                || Level.FINER.equals(level) || Level.FINEST.equals(level))
        {
            severity = IStatus.INFO;
        }
        else if (Level.WARNING.equals(level))
        {
            severity = IStatus.WARNING;
        }
        else if (Level.SEVERE.equals(level))
        {
            severity = IStatus.ERROR;
        }

        String message = record.getMessage();

        Throwable t = record.getThrown();

        Status status = new Status(severity, mPluginID, Status.OK, NLS.bind("Prefix", message), t);
        mPluginLog.log(status);
    }

    /**
     * Not used method
     */
    public void close()
    {
    }

    /**
     * Not used method
     */
    public void flush()
    {
    }

}
